package com.maple.utils.store

import android.text.TextUtils
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson
import com.maple.model.data.AioUserInfo
import com.maple.model.data.UsbDevice
import com.maple.utils.const.UtilsInit
import com.maple.utils.util.LogUtils
import com.tencent.mmkv.MMKV

class AioDBHelper private constructor() {

    companion object {
        private const val KEY_USER: String = "AIO-USER-INFO"
        private const val KEY_PLATFORM: String = "AIO-PLATFORM"
        private const val KEY_REMEMBER_PWD: String = "AIO-REMEMBER-PWD"
        private const val KEY_PASSWORD: String = "AIO-PASSWORD"
        private const val KEY_ACCOUNT: String = "AIO-ACCOUNT"
        private const val KEY_USB_DEVICE: String = "AIO-USB-DEVICE"
        private const val KEY_INDUSTRY_NAME: String = "AIO-INDUSTRY-NAME"
        private const val KEY_TRANSFORM_NAME: String = "AIO-TRANSFORM-NAME"
        private const val KEY_SCALES_NO: String = "AIO-SCALES-NO"

        private val kv: MMKV? by lazy { MMKV.defaultMMKV() }
        private val gson: Gson? by lazy { Gson() }

        private val spUtils: SPUtils by lazy {
            SPUtils.getInstance(UtilsInit.packageName)
        }

        fun saveUser(user: AioUserInfo?): Boolean {
            if (null != user) {
                try {
                    return kv?.encode(KEY_USER, gson?.toJson(user)) ?: false
                } catch (e: Exception) {
                    e.fillInStackTrace()
                    LogUtils.logGGQ("--保存用户异常-->${e.fillInStackTrace()}")
                }
            }
            return false
        }


        fun getUser(): AioUserInfo? {
            val userInfo: String? = kv?.getString(KEY_USER, "")
            if (!TextUtils.isEmpty(userInfo)) {
                try {
                    return gson?.fromJson<AioUserInfo>(userInfo, AioUserInfo::class.java)
                } catch (e: Exception) {
                    e.fillInStackTrace()
                    LogUtils.logGGQ("--获取用户异常-->${e.fillInStackTrace()}")
                }
            }
            return null
        }

        fun removeUser() {
            kv?.remove(KEY_USER)
        }

        fun updateUser(userInfo: AioUserInfo?): Boolean {
            return saveUser(userInfo)
        }

        fun savePlatform(platform: Int?) {
            kv?.putInt(KEY_PLATFORM, platform?:0)
        }

        fun getPlatform(): Int {
            return kv?.getInt(KEY_PLATFORM,0)?: 0
        }

        fun saveRememberPwd(isRememberPwd: Boolean?) {
            kv?.putBoolean(KEY_REMEMBER_PWD, isRememberPwd?:false)
        }

        fun getRememberPwd(): Boolean {
            return kv?.getBoolean(KEY_REMEMBER_PWD,false)?: false
        }

        fun saveLoginInfo(account: String,password: String,platform: Int) {
            saveAccount(account)
            savePassword(password)
            savePlatform(platform)
        }

        fun savePassword(password: String?) {
            kv?.putString(KEY_PASSWORD, password?:"")
        }

        fun getPassword(): String {
            return kv?.getString(KEY_PASSWORD,"")?: ""
        }

        fun clearPassword() {
            savePassword("")
        }

        fun saveAccount(account: String?) {
            kv?.putString(KEY_ACCOUNT, account?:"")
        }

        fun getAccount(): String {
            return kv?.getString(KEY_ACCOUNT,"")?: ""
        }

        fun saveUsbDevice(device: UsbDevice?): Boolean {
            if (null != device) {
                try {
                    return kv?.encode(KEY_USB_DEVICE, gson?.toJson(device)) ?: false
                } catch (e: Exception) {
                    e.fillInStackTrace()
                    LogUtils.logGGQ("--保存USB设备异常-->${e.fillInStackTrace()}")
                }
            }
            return false
        }

        fun getUsbDevice(): UsbDevice? {
            val usbDevice: String? = kv?.getString(KEY_USB_DEVICE, "")
            if (!TextUtils.isEmpty(usbDevice)) {
                try {
                    return gson?.fromJson<UsbDevice>(usbDevice, UsbDevice::class.java)
                } catch (e: Exception) {
                    e.fillInStackTrace()
                    LogUtils.logGGQ("--获取USB设备异常-->${e.fillInStackTrace()}")
                }
            }
            return null
        }
        fun clearAccount() {
            saveAccount("")
        }

        fun saveIndustryName(name: String?) {
            kv?.putString(KEY_INDUSTRY_NAME, name?:"")
        }
        fun getIndustryName(): String {
            return kv?.getString(KEY_INDUSTRY_NAME,"")?: ""
        }
        fun saveTransformName(name: String?) {
            kv?.putString(KEY_TRANSFORM_NAME, name?:"")
        }
        fun getTransformName(): String {
            return kv?.getString(KEY_TRANSFORM_NAME,"")?: ""
        }

        fun saveScalesNo(scalesNo: String?) {
            kv?.putString(KEY_SCALES_NO, scalesNo?:"")
        }

        fun getScalesNo(): String {
            return kv?.getString(KEY_SCALES_NO,"")?: ""
        }
    }

}